package com.xtaifghy.wugui;

import com.xtaifghy.wugui.util.Util;

import java.util.Random;
import java.util.ArrayList;

public class GameState333 implements GameStateInterface{
    private int state = 0xff;
    private int goalState = 0x0f;
    Random rand = new Random();
    private static int[] actions={
            Integer.valueOf("1011",2),//{0,1,3},//1
            Integer.valueOf("111",2),//{0,1,2},//2
            Integer.valueOf("10110",2),//{1,2,4},//3
            Integer.valueOf("101001",2),//{0,5,3},//8
            Integer.valueOf("1011010",2),//{1,4,6,3},//0
            Integer.valueOf("10010100",2),//{2,4,7},//4
            Integer.valueOf("1101000",2),//{6,5,3},//7
            Integer.valueOf("11100000",2),//{6,5,7},//6
            Integer.valueOf("11010000",2),//{6,4,7},//5
    };

    public void doAction(int a){
        state = state^actions[a];
    }

    public boolean testState(){
        return(state==goalState);
    }

    public void setState(boolean[] state) {
        this.state = Util.bArrToInt(state);
    }

    public void setGoalState(boolean[] goalState) {
        this.goalState = Util.bArrToInt(goalState);
    }

    public ArrayList<boolean[]> getPaths(){
        ArrayList<boolean[]> paths = new ArrayList<>();
        expandNode(new boolean[9],0,paths);
        return paths;
    }

    private void expandNode(boolean[] action, int depth,ArrayList<boolean[]>paths){
        //if(paths.size()>0) return;
        if(depth==9){
            if(testAction(state,goalState,action)){
                paths.add(action.clone());
            }
        }else if(depth<9){
            action[depth]=false;
            expandNode(action,depth+1,paths);
            action[depth]=true;
            expandNode(action,depth+1,paths);
        }
    }

    public static boolean testAction(int initState, int goalState, boolean[] actionSeq){
        for(int i=0;i<actionSeq.length;i++){
            if(actionSeq[i]) initState = initState ^ actions[i];
        }
        return (initState==goalState);
    }

    public GameState333() {
        randomGoalState();
    }

    public boolean[] getState() {
        return Util.intToBArr(state,8);
    }

    public boolean[] getGoalState() {
        return Util.intToBArr(goalState,8);
    }

    public void randomGoalState(){
            goalState = rand.nextInt(256);
    }
}
